<?php

class AcaoDao extends Dao {

    public function injectDependencies() {

    }

    /**
     * @method  findAll
     *
     * @since
     * @return  Array de Acao
     * @author  Richard Brandao <richardsbrandao@gmail.com>
     */
    public function findAll() {
        $tQuery = $this->executeSql( TQuery::getSqlFindAllActions() );
        $ArActions = array();
        while($ArAcao = mysqli_fetch_assoc($tQuery)) {
            $ArActions[] = new Acao($ArAcao['IDAcao'], $ArAcao['IDPerfil'], $ArAcao['StAcao'], $ArAcao['StDescricao'], $ArAcao['EnRestrito'], $this->tMysqli);
        }
        return $ArActions;
    }

    public function findByProfile($IDPerfil) {
        $this->initConnection();
        $this->tMysqli->escape($IDPerfil);
        $StSql = TQuery::getSqlFindActionsByProfile($IDPerfil);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArActions = array();
        while($ArAcao = mysqli_fetch_assoc($tQuery)) {
            $ArActions[] = new Acao($ArAcao['IDAcao'], $IDPerfil, $ArAcao['StAcao'], $ArAcao['StDescricao'], $ArAcao['EnRestrito'], $tthis->tMysqli);
        }
        $this->closeConnection();
        return $ArActions;
    }

    public function findRestrictActions() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindAllRestrictActions();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArRestrictActions = array();
        while($ArAcao = mysqli_fetch_assoc($tQuery)) {
            $ArRestrictActions[] = new Acao($ArAcao['IDAcao'], false, $ArAcao['StAcao'], $ArAcao['StDescricao'], $ArAcao['EnRestrito'], $this->tMysqli);
        }
        $this->closeConnection();
        return $ArRestrictActions;
    }

    public function deleteActionsFromProfile($ArIdToDelete, $IDPerfil) {
        $this->initTransaction();
        $this->tMysqli->escape($ArIdToDelete);
        $this->tMysqli->escape($IDPerfil);
        foreach($ArIdToDelete as $IDAction) {
            $StSql = TQuery::getSqlDeleteActionToProfile($IDAction, $IDPerfil);
            $this->tMysqli->exec($StSql);
        }
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function insertActionsFromProfile($ArIdToInsert, $IDPerfil) {
        $this->initTransaction();
        $this->tMysqli->escape($ArIdToInsert);
        $this->tMysqli->escape($IDPerfil);
        foreach($ArIdToInsert as $IDAction) {
            $StSql = TQuery::getSqlInsertActionToProfile($IDAction, $IDPerfil);
            $this->tMysqli->exec($StSql);
        }
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function findFreeActions() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindAllFreeActions();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArAcao = array();
        while ($ArAcaoData =  mysqli_fetch_assoc($tQuery)) {
            $ArAcao[] = new Acao($ArAcaoData['IDAcao'], $ArAcaoData['IDPerfil'], $ArAcaoData['StAcao'], $ArAcaoData['StDescricao'], $ArAcaoData['EnRestrito'], $this->tMysqli);
        }
        $this->closeConnection();
        return $ArAcao;
    }

}

?>
